﻿using System;

public class GlobalMembersGame_resource:My_Queue
{

	internal static LCUI_BOOL library_init = DefineConstantsGame_resource.FALSE;
	internal static LCUI_Queue resource_library = new LCUI_Queue();

    internal static void GraphResClass_Destroy(IntPtr arg)
    {
        //GraphResClass_ res_class;
        //res_class = (GraphResClass_)arg;
        //Queue_Destroy(res_class.graph_res);
    }

    ////* 初始化 
    public static int GameGraphRes_Init()
    {
        if (library_init)
        {
            return -1;
        }
        Queue_Init(resource_library, typeof(GraphResClass_), GraphResClass_Destroy);
        library_init = DefineConstantsGame_resource.TRUE;
        return 0;
    }

    ////* 释放全部资源 
    //public static  void GameGraphRes_FreeAll()
    //{
    //	library_init = DefineConstantsGame_resource.FALSE;
    //	Queue_Destroy(resource_library);
    //}

    ////* 查找资源 
    //internal static GraphResItem_ GameGraphRes_FindItem(GraphResClass_ res_class, int item_id)
    //{
    //	int i;
    //	int n;
    //	GraphResItem_ p_item_data;

    //	Queue_Lock(res_class.graph_res);
    //	n = Queue_GetTotal(res_class.graph_res);
    //	for(i =0; i<n; ++i)
    //	{
    //		p_item_data = (GraphResItem_)Queue_Get(res_class.graph_res, i);
    //		if (p_item_data == null)
    //		{
    //			continue;
    //		}
    //		if(p_item_data.id == item_id)
    //		{
    //			Queue_Unlock(res_class.graph_res);
    //			return p_item_data;
    //		}
    //	}
    //	Queue_Unlock(res_class.graph_res);
    //	return null;
    //}

    ////* 查找资源类 
    //internal static GraphResClass_ GameGraphRes_FindClass(int class_id)
    //{
    //	int i;
    //	int n;
    //	GraphResClass_ p_class_data;

    //	Queue_Lock(resource_library);
    //	n = Queue_GetTotal(resource_library);
    //	for(i =0; i<n; ++i)
    //	{
    //		p_class_data = (GraphResClass_)Queue_Get(resource_library, i);
    //		if (p_class_data == null)
    //		{
    //			continue;
    //		}
    //		if(p_class_data.id == class_id)
    //		{
    //			Queue_Unlock(resource_library);
    //			return p_class_data;
    //		}
    //	}
    //	Queue_Unlock(resource_library);
    //	return null;
    //}

    ////* 释放指定类名的资源 
    //public static int GameGraphRes_Free(string class_name)
    //{
    //	int class_id;
    //	GraphResClass_ res_class;
    //	class_id = BKDRHash(class_name);
    //	res_class = GameGraphRes_FindClass(class_id);
    //	if (res_class != null)
    //	{
    //		Queue_Destroy(res_class.graph_res);
    //		return 0;
    //	}
    //	return -1;
    //}

    //internal static void GraphResItem_Destroy(IntPtr arg)
    //{
    //	GraphResItem_ item;
    //	item = (GraphResItem_)arg;
    //	Graph_Free(item.graph);
    //}

    ////* 添加一个新资源类，返回该资源类的标识号 
    //public static int GameGraphRes_AddClass(string class_name)
    //{
    //	int class_id;
    //	GraphResClass_ class_data = new GraphResClass_();
    //	GraphResClass_ p_class_data;

    //	class_id = BKDRHash(class_name);
    //	p_class_data = GameGraphRes_FindClass(class_id);
    //	if(p_class_data == null)
    //	{
    //		class_data.id = class_id;
    //		class_data.class_name = class_name.Substring(0, DefineConstantsGame_resource.MAX_TEXT_LEN);
    //		Queue_Init(class_data.graph_res, typeof(GraphResItem_), GraphResItem_Destroy);
    //		Queue_Lock(resource_library);
    //		Queue_Add(resource_library, class_data);
    //		Queue_Unlock(resource_library);
    //	}
    //	return class_id;
    //}

    ////* 向指定资源类中加入图像资源 
    //public static int GameGraphRes_AddGraph(int class_id, string name, LCUI_Graph_ graph)
    //{
    //	int item_id;
    //	GraphResItem_ p_item;
    //	GraphResItem_ item_data = new GraphResItem_();
    //	GraphResClass_ p_class_data;

    //	p_class_data = GameGraphRes_FindClass(class_id);
    //	DEBUG_MSG("find class resources, id: %d, addr: %p\n", class_id, p_class_data);
    //	if(p_class_data == null)
    //	{
    //		return -1;
    //	}
    //	item_id = BKDRHash(name);
    //	DEBUG_MSG("graph item, name: %s, id: %d\n", name, item_id);
    //	p_item = GameGraphRes_FindItem(p_class_data, item_id);
    //	if(p_item == null)
    //	{
    //		DEBUG_MSG("add new graph item\n");
    //		item_data.id = item_id;
    //		item_data.name = name.Substring(0, DefineConstantsGame_resource.MAX_TEXT_LEN);
    //		item_data.graph = graph;
    //		Queue_Lock(p_class_data.graph_res);
    //		Queue_Add(p_class_data.graph_res, item_data);
    //		Queue_Unlock(p_class_data.graph_res);
    //	}
    //	return 0;
    //}

    ////* 从文件内载入资源 
    //public static int GameGraphRes_LoadFromFile(string filepath)
    //{
    //	FILE fp;
    //	int count;
    //	int i;
    //	int j;
    //	int k;
    //	int class_id;
    //	int n_pixel;
    //	ResFileHeaderData_ header_data = new ResFileHeaderData_();
    //	GraphResDataBlock_ data_block = new GraphResDataBlock_();
    //	LCUI_Graph_ graph_buff = new LCUI_Graph_();

    //	fp = fopen(filepath, "rb");
    //	if(fp == null)
    //	{
    //		return -1;
    //	}
    //	count = fread(header_data, typeof(ResFileHeaderData_), 1, fp);
    //	if(count < 1)
    //	{
    //		fclose(fp);
    //		return -2;
    //	}
    //	// 验证文件头中的标记文本 
    //	if(string.Compare(header_data.mark, DefineConstantsGame_resource.HEADER_MARK_TEXT) != 0)
    //	{
    //		fclose(fp);
    //		return -3;
    //	}
    //	if(header_data.total_number <= 0)
    //	{
    //		fclose(fp);
    //		return -4;
    //	}
    //	DEBUG_MSG("class name: %s\n", header_data.class_name);
    //	class_id = GameGraphRes_AddClass(header_data.class_name);
    //	for(i =0; i<header_data.total_number; ++i)
    //	{
    //		count = fread(data_block, typeof(GraphResDataBlock_), 1, fp);
    //		if(count < 1)
    //		{
    //			break;
    //		}
    //		// 验证数据块的标记文本 
    //		if(string.Compare(data_block.mark, DefineConstantsGame_resource.BLOCK_MARK_TEXT) != 0)
    //		{
    //			// 将文件指针移向读该数据块前的位置的下个位置 
    //			fseek(fp, SEEK_CUR, -(int)(typeof(GraphResDataBlock_)-1));
    //			--i;
    //			continue;
    //		}
    //		Graph_Init(graph_buff);
    //		graph_buff.color_type = data_block.color_type;
    //		if(graph_buff.color_type == (int)GraphColorTyle.COLOR_TYPE_RGBA)
    //		{
    //			k = 4;
    //		}
    //		else
    //		{
    //			k = 3;
    //		}
    //		Graph_Create(graph_buff, data_block.w, data_block.h);
    //		DEBUG_MSG("read graph, color type: %d, width: %d, height: %d\n", graph_buff.color_type, data_block.w, data_block.h);
    //		n_pixel = data_block.w * data_block.h;
    //		// 读取像素数据 
    //		for(j =0; j<k; ++j)
    //		{
    //			count = fread(graph_buff.rgba[j], typeof(byte), n_pixel, fp);
    //			if(count < n_pixel)
    //			{
    //				goto error_exit;
    //			}
    //		}
    //		GameGraphRes_AddGraph(class_id, data_block.name, graph_buff);
    //	}
    //	fclose(fp);
    //	return 0;

    //error_exit:
    //	fclose(fp);
    //	return -5;
    //}

    ////* 将指定类资源写入至文件 
    //public static int GameGraphRes_WriteToFile(string filepath, string class_name)
    //{
    //	FILE fp;
    //	GraphResItem_ p_item;
    //	GraphResDataBlock_ data_block = new GraphResDataBlock_();
    //	GraphResClass_ p_class_data;
    //	ResFileHeaderData_ header_data = new ResFileHeaderData_();
    //	int i;
    //	int n;
    //	int class_id;
    //	int n_pixel;
    //	int count;
    //	int j;
    //	int k;

    //	class_id = BKDRHash(class_name);
    //	p_class_data = GameGraphRes_FindClass(class_id);
    //	DEBUG_MSG("find class resources...\n");
    //	if(p_class_data == null)
    //	{
    //		DEBUG_MSG("class resources not found!\n");
    //		return -1;
    //	}
    //	DEBUG_MSG("class resources: %p\n", p_class_data);
    //	fp = fopen(filepath, "wb+");
    //	if(fp == null)
    //	{
    //		return -2;
    //	}

    //	header_data.mark = DefineConstantsGame_resource.HEADER_MARK_TEXT.Substring(0, DefineConstantsGame_resource.MAX_MARK_TEXT_LEN);
    //	header_data.class_name = class_name.Substring(0, DefineConstantsGame_resource.MAX_TEXT_LEN);
    //	// 保存资源总数 
    //	header_data.total_number = Queue_GetTotal(p_class_data.graph_res);
    //	// 先将文件头数据写进去 
    //	count = fwrite(header_data, typeof(ResFileHeaderData_), 1, fp);
    //	DEBUG_MSG("write file header data...\n");
    //	if(count < 1)
    //	{
    //		DEBUG_MSG("file header data are write error!\n");
    //		fclose(fp);
    //		return -3;
    //	}
    //	Queue_Lock(p_class_data.graph_res);
    //	n = Queue_GetTotal(p_class_data.graph_res);
    //	data_block.mark = DefineConstantsGame_resource.BLOCK_MARK_TEXT.Substring(0, DefineConstantsGame_resource.MAX_MARK_TEXT_LEN);
    //	DEBUG_MSG("total number of graph item: %d\n", n);
    //	for(i =0; i<n; ++i)
    //	{
    //		p_item = (GraphResItem_)Queue_Get(p_class_data.graph_res, i);
    //		if (p_item == null)
    //		{
    //			continue;
    //		}
    //		// 准备本张图像的信息 
    //		data_block.color_type = p_item.graph.color_type;
    //		data_block.w = p_item.graph.w;
    //		data_block.h = p_item.graph.h;
    //		data_block.name = p_item.name.Substring(0, DefineConstantsGame_resource.MAX_TEXT_LEN);
    //		DEBUG_MSG("write graph item: %p, name: %s, size: %d,%d\n", p_item, p_item.name, data_block.w, data_block.h);
    //		// 写入本张图像的信息 
    //		count = fwrite(data_block, typeof(GraphResDataBlock_), 1, fp);
    //		if(count < 1)
    //		{
    //			goto error_exit;
    //		}
    //		n_pixel = data_block.w * data_block.h;
    //		if(p_item.graph.color_type == (int)GraphColorTyle.COLOR_TYPE_RGBA)
    //		{
    //			k = 4;
    //		}
    //		else
    //		{
    //			k = 3;
    //		}
    //		DEBUG_MSG("write graph pixel data, total pixel: %d\n", n_pixel);
    //		// 写入像素数据 
    //		for(j =0; j<k; ++j)
    //		{
    //			count = fwrite(p_item.graph.rgba[j], typeof(byte), n_pixel, fp);
    //			if(count < n_pixel)
    //			{
    //				goto error_exit;
    //			}
    //		}
    //	}
    //	Queue_Unlock(p_class_data.graph_res);
    //	fclose(fp);
    //	DEBUG_MSG("every thing is OK!\n");
    //	return 0;

    //error_exit:
    //;
    //	Queue_Unlock(p_class_data.graph_res);
    //	fclose(fp);
    //	return -4;
    //	;
    //}

    ////* 在指定资源类里面获取指定名称的资源 
    //public static int GameGraphRes_GetGraph(string class_name, string name, LCUI_Graph_ buff)
    //{
    //	GraphResItem_ p_item;
    //	GraphResClass_ p_class_data;
    //	int class_id;
    //	int item_id;

    //	class_id = BKDRHash(class_name);
    //	DEBUG_MSG("find class resources...\n");
    //	p_class_data = GameGraphRes_FindClass(class_id);
    //	if(p_class_data == null)
    //	{
    //		DEBUG_MSG("class resources not found!\n");
    //		return -1;
    //	}
    //	item_id = BKDRHash(name);
    //	p_item = GameGraphRes_FindItem(p_class_data, item_id);
    //	DEBUG_MSG("graph item: %p\n",p_item);
    //	if(p_item == null)
    //	{
    //		return -2;
    //	}
    //	buff = p_item.graph;
    //	return 0;
    //}
}
// ****************************************************************************
// * Punch And Kick -- a simple 2D Fighting Game.
// *
// * Copyright (C) 2013 by
// * Liu Chao
// *
// * This file is part of the Punch And Kick project, and may only be used,
// * modified, and distributed under the terms of the GPLv2.
// *
// * (GPLv2 is abbreviation of GNU General Public License Version 2)
// *
// * By continuing to use, modify, or distribute this file you indicate that you
// * have read the license and understand and accept it fully.
// *
// * The Punch And Kick project is distributed in the hope that it will be
// * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GPL v2 for
// * more details.
// *
// * You should have received a copy of the GPLv2 along with this file. It is
// * usually in the LICENSE.TXT file, If not, see <http://www.gnu.org/licenses/>.
// * **************************************************************************

// ****************************************************************************
// * Punch And Kick -- 一个普通的2D格斗游戏
// *
// * 版权所有 (C) 2013 归属于
// * 刘超
// *
// * 这个文件是 Punch And Kick 项目的一部分，并且只可以根据GPLv2许可协议来使用、
// * 更改和发布。
// *
// * (GPLv2 是 GNU通用公共许可证第二版 的英文缩写)
// *
// * 继续使用、修改或发布本文件，表明您已经阅读并完全理解和接受这个许可协议。
// *
// * Punch And Kick 项目是基于技术交流目的而加以散布的，但不负任何担保责任，例
// * 如：稳定性、可玩性、趣味性等的担保，甚至没有适销性或特定用途的隐含担保，详
// * 情请参照GPLv2许可协议。
// *
// * 您应已收到附随于本文件的GPLv2许可协议的副本，它通常在LICENSE.TXT文件中，如果
// * 没有，请查看：<http://www.gnu.org/licenses/>.
// * **************************************************************************

// 游戏资源管理模块



////*文件头部的标记文本 
//#define HEADER_MARK_TEXT
////* 每个资源数据块的标记文本 
//#define BLOCK_MARK_TEXT

//#define MAX_MARK_TEXT_LEN
//#define MAX_TEXT_LEN

public class ResFileHeaderData_
{
	public string mark = new string(new char[DefineConstantsGame_resource.MAX_MARK_TEXT_LEN]);
	public int total_number;
	public string class_name = new string(new char[DefineConstantsGame_resource.MAX_TEXT_LEN]);
}

public class GraphResDataBlock_
{
	public string mark = new string(new char[DefineConstantsGame_resource.MAX_MARK_TEXT_LEN]);
	public string name = new string(new char[DefineConstantsGame_resource.MAX_TEXT_LEN]);
	public int w;
	public int h;
	public int color_type;
}

public class GraphResItem_
{
	public int id;
	public string name = new string(new char[DefineConstantsGame_resource.MAX_TEXT_LEN]);
	public LCUI_Graph_ graph = new LCUI_Graph_();
}

public class GraphResClass_
{
	public int id;
	public string class_name = new string(new char[DefineConstantsGame_resource.MAX_TEXT_LEN]);
	public LCUI_Queue graph_res = new LCUI_Queue();
}
